<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
// load base class if needed
require_once( APPPATH . 'controllers/base/OperatorBase.php' );

class operator extends ApplicationBase {

    // constructor
    public function __construct() {
        parent::__construct();
        // load model
        $this->load->model('m_operator_mini');
        $this->load->model('m_kategoribarang');
        // load library
        $this->load->library('tnotification');
        $this->load->library('pagination');
        // load helpers
    }

    // welcome administrator
    public function index() {
        // set template content
        $this->smarty->assign("template_content", "pengaturan/operator/list.html");
        /* start of pagination --------------------- */
        // pagination
        $params = array();
        $config['base_url'] = site_url("master/operator/index/");
        $config['total_rows'] = $this->m_operator_mini->get_total_operator();
        $config['uri_segment'] = 4;
        $config['per_page'] = 10;
        $this->pagination->initialize($config);
        $pagination['data'] = $this->pagination->create_links();
        // pagination attribute
        $start = $this->uri->segment(4, 0) + 1;
        $end = $this->uri->segment(4, 0) + $config['per_page'];
        $end = (($end > $config['total_rows']) ? $config['total_rows'] : $end);
        $pagination['start'] = ($config['total_rows'] == 0) ? 0 : $start;
        $pagination['end'] = $end;
        $pagination['total'] = $config['total_rows'];
        // pagination assign value
        $this->smarty->assign("pagination", $pagination);
        $this->smarty->assign("no", $start);
        /* end of pagination ---------------------- */
        // get data
        $params = array(($start - 1), $config['per_page']);
        $this->smarty->assign("rs_id", $this->m_operator_mini->get_all_operator($params));

        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    public function add() {
        // $this->_set_page_rule("R");
        // set template content
        $this->smarty->assign("template_content", "pengaturan/operator/add.html");

        //data
        $rs_com_role = $this->m_operator_mini->get_all_role();
        $this->smarty->assign("rs_com_role", $rs_com_role);

        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();

        // echo "dadasd" . $this->m_operator_mini->is_exist_username("ha");
        // output
        parent::display();
    }

    // add process
    public function add_process() {
        // cek input 

        $this->tnotification->set_rules('user_name', 'Username', 'trim|required');
        $this->tnotification->set_rules('user_pass', 'Password', 'trim|required');
        // $this->tnotification->set_rules('user_mail', 'Email', 'trim|required');

        $this->tnotification->set_rules('petugas_name', 'Nama', 'trim|required');
        //$this->tnotification->set_rules('petugas_jabatan', 'Jabatan', 'trim|required');
        // $this->tnotification->set_rules('petugas_telepon', 'Telepon', 'trim|required');

        $this->tnotification->set_rules('petugas_alamat', 'Alamat', 'trim|required');
        $this->tnotification->set_rules('role_id', 'Role', 'trim|required');

        $username = trim($this->input->post('user_name'));

        // cek username    
        if ($this->m_operator_mini->is_exist_username($username) != NULL) {
            $this->tnotification->sent_notification("error", 'Username is not available');
        }

        if ($this->tnotification->run() !== FALSE) {
            $password_key = crc32($this->input->post('user_pass'));
            $password = $this->encrypt->encode($this->input->post('user_pass'), $password_key);

            $params = array(
                $username,
                $password,
                $password_key,
                "0",
                $this->input->post('user_mail'),
                $this->input->post('petugas_name'),
                $this->input->post('petugas_jabatan'),
                $this->input->post('petugas_telepon'),
                $this->input->post('petugas_alamat'),
                $this->com_user['user_id']
            );

            //insert
            if ($this->m_operator_mini->insert_operator($params)) {
                //insert role
                $user_id = $this->m_operator_mini->get_users_id_by_username($username);

                $params = array($this->input->post('role_id'), $user_id);
                $this->m_operator_mini->insert_role($params);

                // notification
                $this->tnotification->delete_last_field();
                $this->tnotification->sent_notification("success", "Data berhasil disimpan");
            } else {
                // default error
                $this->tnotification->sent_notification("error", "Data gagal disimpan");
            }
        } else {
            // default error
            $this->tnotification->sent_notification("error", "Data gagal disimpan");
        }
        // default redirect
        redirect("pengaturan/operator/add");
    }

    public function edit($id) {
        // $this->_set_page_rule("R");
        // set template content
        $this->smarty->assign("template_content", "pengaturan/operator/edit.html");

        //data
        $result = $this->m_operator_mini->get_operator_by_id($id);
        $this->smarty->assign("result", $result);

        //data
        $rs_com_role = $this->m_operator_mini->get_all_role();
        $this->smarty->assign("rs_com_role", $rs_com_role);

        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    public function edit_process() {

        //get data
        $this->tnotification->set_rules('user_name', 'Username', 'trim|required');
        $this->tnotification->set_rules('user_pass', 'Password', 'trim|required');
        // $this->tnotification->set_rules('user_mail', 'Email', 'trim|required');
        $this->tnotification->set_rules('petugas_name', 'Nama', 'trim|required');
        //$this->tnotification->set_rules('petugas_jabatan', 'Jabatan', 'trim|required');
        // $this->tnotification->set_rules('petugas_telepon', 'Telepon', 'trim|required');
        $this->tnotification->set_rules('petugas_alamat', 'Alamat', 'trim|required');
        $this->tnotification->set_rules('role_id', 'Role', 'trim|required');
        $username = trim($this->input->post('user_name'));

        // cek username         
        if ($this->m_operator_mini->is_exist_username($username) != NULL) {
            $this->tnotification->sent_notification("error", 'Username is not available');
        }

        if ($this->tnotification->run() !== FALSE) {
            $password_key = crc32($this->input->post('user_pass'));
            $password = $this->encrypt->encode($this->input->post('user_pass'), $password_key);

            $params = array(
                $username,
                $password,
                $password_key,
                "0",
                $this->input->post('user_mail'),
                $this->input->post('petugas_name'),
                $this->input->post('petugas_jabatan'),
                $this->input->post('petugas_telepon'),
                $this->input->post('petugas_alamat'),
                $this->input->post('user_img'),
                $this->com_user['user_id'],
                $this->input->post('user_id')
            );

            // insert
            if ($this->m_operator_mini->edit($params)) {

                //delete role
                $this->m_operator_mini->delete_role($this->input->post('user_id'));
                //insert role
                $params = array($this->input->post('role_id'), $this->input->post('user_id'));
                $this->m_operator_mini->insert_role($params);


                // notification
                $this->tnotification->delete_last_field();
                $this->tnotification->sent_notification("success", "Data berhasil disimpan");
            } else {
                // default error
                $this->tnotification->sent_notification("error", "Data gagal disimpan");
            }
        } else {
            // default error
            $this->tnotification->sent_notification("error", "Data gagal disimpan");
        }
        // default redirect
        redirect("pengaturan/operator/edit/" . $this->input->post('user_id'));
    }

    public function delete($id) {
        // $this->_set_page_rule("R");
        // set template content
        $this->smarty->assign("template_content", "pengaturan/operator/delete.html");
        //data
        $result = $this->m_operator_mini->get_operator_by_id($id);
        $this->smarty->assign("result", $result);
        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    public function delete_process() {
        // cek input 
        $this->tnotification->set_rules('user_id', 'Kode', 'trim|required|max_length[3]');
        // process
        if ($this->tnotification->run() !== FALSE) {
            // parameter
            $params = array($this->input->post('user_id')
            );
            // insert
            if ($this->m_operator_mini->delete($params)) {
                $this->m_operator_mini->delete_role($this->input->post('user_id'));
                // notification
                $this->tnotification->delete_last_field();
                $this->tnotification->sent_notification("success", "Data berhasil disimpan");
            } else {
                // default error
                $this->tnotification->sent_notification("error", "Data gagal disimpan");
            }
        } else {
            // default error
            $this->tnotification->sent_notification("error", "Data gagal disimpan");
        }
        // default redirect
        redirect("pengaturan/operator/");
    }

}
